﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model;
using BLL;

namespace Demo.Frm
{
    public partial class FrmEmployeeAdd : Form
    {
        //员工
        EmployeeModel _employeemodel = new EmployeeModel();
        EmployeeBLL _employeebll = new EmployeeBLL();

        //部门
        DepartmentModel _departmentmodel = new DepartmentModel();
        DepartmentBLL _departmentbll = new DepartmentBLL();
        private string _ID = "";
        public FrmEmployeeAdd()
        {
            InitializeComponent();
            this.ControlBox = false;
        }

        public FrmEmployeeAdd(string id)
        {
            InitializeComponent();
            this.ControlBox = false;
            _ID = id;
            this.Text = "修改员工信息";
        }

        private void FrmEmployeeAdd_Load(object sender, EventArgs e)
        {
            //控制日期或时间的显示格式
            this.dateEmployeeBirth.CustomFormat = "yyyy-MM-dd";
            //加载部门下拉列表
            IEnumerable<DepartmentModel> departmentList = _departmentbll.GetList();
            combDepartment.DataSource = departmentList;
            combDepartment.DisplayMember = "DepartmentName";
            combDepartment.ValueMember = "DepartmentNo";
            combDepartment.SelectedIndex = -1;
            combDepartment.Text = "请选择部门";
            if (_ID != "")
            {
                loadData();
            }
        }

        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripbtnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripbtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 编辑时初始化加载数据
        /// </summary>
        public void loadData()
        {
            _employeemodel = _employeebll.Get(_ID);
            this.txtEmployeeNo.Text = _employeemodel.EmployeeNo;
            this.txtEmployeeName.Text = _employeemodel.EmployeeName;
            this.txtRemarks.Text = _employeemodel.Remarks;
            //获取该员工对应的部门
            DepartmentModel departmentmodel = _departmentbll.Get(_employeemodel.DepartmentId);
            this.combDepartment.SelectedValue = departmentmodel.DepartmentNo;
            //加载员工性别
            if (_employeemodel.EmployeeSex == "男")
            {
                this.radiomen.Checked = true;
            }
            else
            {
                this.radiowoman.Checked = true;
            }
            this.dateEmployeeBirth.Text = _employeemodel.EmployeeBirth.ToString();
            //加载员工是否在职
            if (_employeemodel.IsJob == 1)
            {
                cbIsJob.Checked = true;
            }
            else
            {
                cbIsJob.Checked = false;
            }
        }

        /// <summary>
        /// 保存信息
        /// </summary>
        public void Save()
        {
            //判断必填项有没有填
            int flag = IsCheck();
            if (flag != 0)
            {
                if (flag == 1)
                {
                    errorPEmployeeNo.SetError(txtEmployeeNo,"员工编码不能为空");
                }
                else if (flag==2)
                {
                    errorPEmployeeName.SetError(txtEmployeeName, "员工姓名不能为空");
                }
                else if (flag == 3)
                {
                    errorPDepartment.SetError(combDepartment, "部门不能为空");
                }
                return;
            }
            //判断是新增还是修改
            if (_ID == "")
            {
                //根据员工编码判断此员工编码是否已经存在
                int exist = _employeebll.IsExistByEmployeeNo(this.txtEmployeeNo.Text);
                if (exist==-1)
                {
                    //不存在就给实体赋值
                    EmployeeModel employeemodel = SetData();
                    employeemodel.Id = Guid.NewGuid().ToString();
                    //添加数据到数据库
                    _employeebll.Insert(employeemodel);
                }
                else
                {
                    MessageBox.Show("员工编码已存在，请重新输入员工编码");
                    return;
                }
            }
            else
            {
                //根据当前Id和员工编码判断此员工编码是否已经存在
                int exist = _employeebll.IsExistByIdAndEmployeeNo(_ID, this.txtEmployeeNo.Text);
                if (exist==-1)
                {
                    //不存在就给实体赋值
                    EmployeeModel employeemodel = SetData();
                    employeemodel.Id = _ID;
                    //更新数据库数据
                    _employeebll.Update(employeemodel);
                }
                else
                {
                    MessageBox.Show("员工编码已存在，请重新输入员工编码");
                    return;
                }
            }
            this.DialogResult = DialogResult.OK;
            MessageBox.Show("保存成功");
            this.Close();
        }

        /// <summary>
        /// 给实体赋值
        /// </summary>
        /// <returns></returns>
        public EmployeeModel SetData()
        {
            EmployeeModel employeemodel = new EmployeeModel();
            //给实体赋值
            employeemodel.EmployeeNo = this.txtEmployeeNo.Text;
            employeemodel.EmployeeName = this.txtEmployeeName.Text;
            employeemodel.DepartmentId = _departmentbll.GetModelByDepartmentNo(this.combDepartment.SelectedValue.ToString()).Id;
            if (this.radiomen.Checked == true)
            {
                employeemodel.EmployeeSex = "男";
            }
            else
            {
                employeemodel.EmployeeSex = "女";
            }
            if (this.cbIsJob.Checked)
            {
                employeemodel.IsJob = 1;
            }
            else
            {
                employeemodel.IsJob = 0;
            }
            employeemodel.EmployeeBirth = Convert.ToDateTime(this.dateEmployeeBirth.Text);
            employeemodel.Remarks = this.txtRemarks.Text;
            return employeemodel;
        }

        /// <summary>
        /// 检查必填项是否都填了
        /// </summary>
        /// <returns></returns>
        public int IsCheck()
        {
            errorPEmployeeNo.SetError(txtEmployeeNo,"");
            errorPEmployeeName.SetError(txtEmployeeName,"");
            errorPDepartment.SetError(combDepartment,"");
            if (this.txtEmployeeNo.Text == "")
            {
                return 1;
            }
            if (this.txtEmployeeName.Text == "")
            {
                return 2;
            }
            if (this.combDepartment.Text == "")
            {
                return 3;
            }
            return 0;
        }

    }
}
